<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/4/29 14:30
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);

namespace app\mall\model\traits;

use app\mall\enum\OpUserEnum;
use app\mall\model\MallStoreClerk;
use app\mall\model\MallUser;
use suframe\thinkAdmin\model\AdminUsers;
use think\Model;

/**
 * OpUserTrait
 * trait OpUserTrait
 * @package app\mall\model\traits
 * @mixin Model
 */
trait OpUserTrait
{
    /**
     * @param $key
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserAttr($key, $data)
    {
        $rs = [];
        switch ($data['user_type']) {
            case OpUserEnum::USER:
                /** @var MallUser $user */
                $user = MallUser::find($data['uid']);
                $rs = [
                    'id' => $user->value('id'),
                    'name' => $user->value('nickname'),
                    'avatar' => $user->value('avatar'),
                ];
                break;
            case OpUserEnum::ADMIN:
                $user =  AdminUsers::find($data['uid']);
                $rs = [
                    'id' => $user->value('id'),
                    'name' => $user->value('real_name'),
                    'avatar' => $user->value('avatar'),
                ];
                break;
            case OpUserEnum::CLERK:
                $user = MallStoreClerk::find($data['uid']);
                $rs = [
                    'id' => $user->value('id'),
                    'name' => $user->value('name'),
                    'avatar' => $user->value('avatar'),
                ];
                break;
        }
        if($rs){
            $rs['type'] = $data['user_type'];
            $rs['type_text'] = (new OpUserEnum($data['user_type']))->getZhName();
        }
        return $rs ?: null;
    }
}